﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_topic_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlCommunity.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlCommunity.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.DelTopic(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetTopic(int.Parse(hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    this.ddlCommunity.Items.FindByValue(dt.Rows[0]["self_community_code"].ToString()).Selected = true;
                    this.ddlVoteType.Items.FindByValue(dt.Rows[0]["votetype"].ToString()).Selected = true;
                    this.txtTitle.Text = dt.Rows[0]["title"].ToString();
                    this.txtBeginDate.Text = dt.Rows[0]["begindate"].ToString();
                    this.txtEndDate.Text = dt.Rows[0]["enddate"].ToString();
                    this.editor_id.Value = dt.Rows[0]["content"].ToString();
                    bindSubItem();
                }
            }
        }

        private void bindSubItem()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.dgdSubItem.DataSource = dal.GetTopicVoteItemList(int.Parse(this.hidId.Value));
            this.dgdSubItem.DataBind();
        }


        private bool SaveData()
        {
            string sCommunity = this.ddlCommunity.SelectedValue;
            string sType = Request.QueryString["t"] == "1" ? "社区议题" : Request.QueryString["t"] == "2" ? "业主大会" : "维修基金";
            string sTitle = this.txtTitle.Text.Trim();
            string sBeginDate = this.txtBeginDate.Text.Trim();
            string sEndDate = this.txtEndDate.Text.Trim();
            string sContent = this.editor_id.Value;
            string sVote = this.ddlVoteType.Text;
            DateTime bDD = DateTime.Today;
            DateTime eDD = DateTime.MaxValue;
            if (sTitle == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "标题不能为空！");
                return false;
            }
            if (sBeginDate != string.Empty)
            {
                if (!DateTime.TryParse(sBeginDate, out bDD))
                {
                    PM.Web.Common.JsAlert(this, "开始日期输入不正确！");
                    return false;
                }
            }
            if (sEndDate != string.Empty)
            {
                if (!DateTime.TryParse(sEndDate, out eDD))
                {
                    PM.Web.Common.JsAlert(this, "结束日期输入不正确！");
                    return false;
                }
            }

            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                int newId = 0;
                if ((newId = dal.AddTopic(int.Parse(sCommunity), 2, sType, sTitle, sContent, bDD, eDD, sVote)) > 0)
                {
                    this.hidId.Value = newId.ToString();
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return dal.ModifyTopic(int.Parse(hidId.Value), int.Parse(sCommunity), 2, sType, sTitle, sContent, bDD, eDD, sVote);
            }
            return true;
        }

        protected void btnSubSave_Click(object sender, EventArgs e)
        {
            if (SaveData())
            {
                string sSubTitle = this.txtSubTitle.Text.Trim();
                DataAccess.PmData dal = new DataAccess.PmData();
                dal.AddTopicVoteItem(int.Parse(this.hidId.Value), sSubTitle);
                bindSubItem();
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            if (SaveData())
            {
                PM.Web.Common.JsAlert(this, "保存成功！", true);
            }
        }

        protected void dgdSubItem_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string sCommandName = e.CommandName;
            int id = Convert.ToInt32(dgdSubItem.DataKeys[e.Item.ItemIndex]);
            DataAccess.PmData dal = new DataAccess.PmData();
            switch (sCommandName)
            {
                case "Edit":
                    this.dgdSubItem.EditItemIndex = e.Item.ItemIndex;
                    bindSubItem();
                    break;
                case "Update":
                    string sSubTitle = ((TextBox)e.Item.FindControl("txtEditSubName")).Text.Trim();
                    if (sSubTitle == string.Empty)
                    {
                        PM.Web.Common.JsAlert(this, "投票项名称不能为空！");
                        return;
                    }
                    dal.ModifyTopicVoteItem(id, sSubTitle);
                    this.dgdSubItem.EditItemIndex = -1;
                    bindSubItem();
                    break;
                case "Delete":
                    dal.DelTopicVoteItem(id);
                    bindSubItem();
                    break;
                case "Cancel":
                    this.dgdSubItem.EditItemIndex = -1;
                    bindSubItem();
                    break;
            }
        }
    }
}